﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    OutlookStorageItem.cs
//
// Abstract:
//    
//    Implements the IStorageItem interface using Outlook StorageItem data.
//
//--

using System.Diagnostics;
using Microsoft.Office.Interop.Outlook;

namespace Microsoft.EnhancedReminders.OutlookAddIn
{
    /// <summary>
    /// Implements the IStorageItem interface using an Outlook Storage Item
    /// </summary>
    public sealed class StorageItemWrapper : IStorageItem
    {
        private StorageItem _storageItem;

        /// <summary>
        /// ctor
        /// </summary>
        public StorageItemWrapper(StorageItem storageItem)
        {
            _storageItem = storageItem;
        }

        #region IStorageItem Members

        /// <summary>
        /// Implements IStorageItem.Attachments
        /// </summary>
        public Attachments Attachments
        {
            get { return _storageItem.Attachments; }
        }

        /// <summary>
        /// Implements IStorageItem.Save
        /// </summary>
        public void Save()
        {
            _storageItem.Save();
        }

        #endregion
    }

    /// <summary>
    /// Implements the IStorageItem interface using Outlook MailItem data.
    /// </summary>
    public sealed class OutlookStorageItem : IStorageItem
    {
        private MailItem _storageItem;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="storageItem">Outlook MailItem</param>
        public OutlookStorageItem(MailItem storageItem)
        {
            Debug.Assert(storageItem != null, "storageItem != null");
            _storageItem = storageItem;
        }

        /// <summary>
        /// Implements IStorageItem.Attachments
        /// </summary>
        public Attachments Attachments
        {
            get
            {
                return _storageItem.Attachments;
            }
        }

        /// <summary>
        /// Implements IStorageItem.Save
        /// </summary>
        public void Save()
        {
            Debug.Assert(_storageItem != null, "_storageItem != null");
            _storageItem.Save();
        }

        /// <summary>
        /// Determines if the specified MailItem is a valid storage item.
        /// </summary>
        /// <param name="mailItem">MailItem</param>
        /// <remarks>
        /// A storage item has a specific subject and attachment.
        /// </remarks>
        /// <returns>
        /// true if the specified MailItem is a valid storage item; false otherwise
        /// </returns>
        internal static bool IsValidStorageItem(MailItem mailItem)
        {
            return mailItem != null && mailItem.Subject == Constants.StorageItemName && mailItem.Attachments != null && 
                mailItem.Attachments.Count == 1 && mailItem.Attachments[1 /* 1-based index */].FileName == Constants.AttachmentName;
        }
    }
}
